# -*- coding: utf-8 -*-
import scrapy
from bs4 import BeautifulSoup
import re

class StocksSpider(scrapy.Spider):
    name = 'stocks'
    start_urls = ['http://quote.eastmoney.com/stock_list.html']

    def parse(self, response):
        for href in response.xpath('//a[@target="_blank"]/@href').extract():
            try:
                stock = re.findall(r"[s][hz]\d{6}",href)[0]
                url = 'http://gu.qq.com/' + stock
                yield scrapy.Request(url, callback=self.parse_stock)

            except:
                continue
            

    def parse_stock(self,response):
        infoDict = {}
        stockname = response.xpath('//h1[@class="col-1-1"]/text()').get()
        stockval = response.xpath('//span[@class="data"]/text()').get()
        infoDict.update({'股票名称': stockname})
        infoDict.update({'股票价格': stockval})
        for k,v in infoDict.items():
            print(k,v)

        yield infoDict

